home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / Sample Code / Snippets / AOCE / Construct AOCE Address / ConstructMyAddress.c
Encoding:
C/C++ Source or Header  |  1994-08-09  |  4.7 KB  |  158 lines  |  [TEXT/MPS ]

  1. /*****************************************************************
  2.  *    ConstructMyAddress.c
  3.  *
  4.  *    8/8/94
  5.  *    By: Scott Kuechle
  6.  *    Apple Developer Technical Support
  7.  *
  8.  *    Written in MPW 3.3.1 C
  9.  *    Compiles with 68K interfaces
  10.  *
  11.  *    Description:
  12.  *    
  13.  *    Creates an AOCE packedDSSpec structure representing the address of the mailbox
  14.  *    of the machine that it is running on. This could then, for example, be passed to
  15.  * SMPResolveToRecipient to create a mail address for the given machine.
  16.  *
  17.  *****************************************************************/
  18.  
  19.  
  20. #include <AppleTalk.h>
  21. #include <OCE.h>
  22. #include <OCEMessaging.h>
  23. #include <errors.h>
  24. #include <TextUtils.h>
  25. #include <Resources.h>
  26.  
  27. OSErr ConstructMyAddress(EntityPtr          entityPtr,
  28.                           PackedDSSpecPtr  *packedDSSpecPtr,
  29.                           OCERecipient     *recipient);
  30. OSErr BuildOurEntity(EntityPtr entityPtr);
  31. OSErr GetOurZoneName(StringPtr zonePtr);
  32.  
  33.    /* globals */
  34. #define   gThisZone  "\p*"
  35.  
  36. /*****************************************************************
  37.  *    ConstructMyAddress
  38.  *
  39.  *  Build an NBP EntityName structure representing this machine and then use that to
  40.  *  construct a packedDSSpec (or OCERecipient ) structure.
  41.  *****************************************************************/
  42.  
  43. OSErr ConstructMyAddress(EntityPtr          entityPtr,
  44.                           PackedDSSpecPtr  *packedDSSpecPtr,
  45.                           OCERecipient     *recipient)
  46. {
  47. OSErr         err;
  48. RString32     *recordName;
  49. unsigned short     packedSpecSize;
  50. RecordIDPtr    ridPtr;
  51.  
  52.  
  53.     err = BuildOurEntity(entityPtr);
  54.     if (err == noErr)
  55.     {
  56.        ridPtr = (RecordIDPtr)NewPtr(sizeof(RecordID));
  57.        err = MemError();
  58.        if (err == noErr)
  59.        {
  60.           ridPtr->rli = NULL;
  61.           recipient->entitySpecifier = ridPtr;
  62.           OCESetCreationIDtoNull(&ridPtr->local.cid);
  63.           ridPtr->local.recordType = OCEGetIndRecordType(kUserRecTypeNum);
  64.           recordName = (RString32 *) NewPtr(sizeof(RString32));
  65.           err = MemError();
  66.           if (err == noErr)
  67.           {
  68.              OCEPToRString(&entityPtr->objStr,
  69.                             smRoman,
  70.                             (RStringPtr)recordName,
  71.                             kRString32Size);
  72.              ridPtr->local.recordName = (RStringPtr)recordName;
  73.  
  74.              recipient->extensionType = kOCEalanXtn;
  75.                 /* extension is the entity name */
  76.              recipient->extensionSize = (entityPtr->objStr[0] + 1) +
  77.                                       (entityPtr->typeStr[0] + 1) +
  78.                                       (entityPtr->zoneStr[0] + 1);
  79.              recipient->extensionValue = (Ptr)entityPtr;
  80.              
  81.                 /* construct a packedDSSpec */
  82.              packedSpecSize = OCEPackedDSSpecSize(recipient);
  83.              *packedDSSpecPtr = (PackedDSSpecPtr)NewPtr(packedSpecSize);
  84.              err = MemError();
  85.              if (err == noErr)
  86.                 err = OCEPackDSSpec(recipient, *packedDSSpecPtr, packedSpecSize);
  87.            }
  88.        }
  89.     }
  90.     return (err);
  91. }
  92.  
  93. /*****************************************************************
  94.  *    BuildOurEntity
  95.  *
  96.  *  Builds an AppleTalk NBP EntityName structure representing this machine.
  97.  *****************************************************************/
  98.  
  99. OSErr BuildOurEntity(EntityPtr entityPtr)
  100. {
  101. StringHandle  userName;
  102. OSErr        err;
  103. short         resError;
  104.  
  105.  
  106.     err = GetOurZoneName(&entityPtr->zoneStr[0]);
  107.     if (err == noErr)
  108.     {
  109.        BlockMove(kIPMWSReceiverNBPType,
  110.                   &entityPtr->typeStr[0],
  111.                   kIPMWSReceiverNBPType[0]+1);
  112.           /* get flagship name */
  113.        userName = GetString(-16413);
  114.        if (userName != NULL)
  115.           BlockMove(*userName,
  116.                      &entityPtr->objStr[0],
  117.                      *userName[0]+1);
  118.        else
  119.        {
  120.           resError = ResError();
  121.           if (resError == noErr)
  122.              return (resNotFound);
  123.           else
  124.              return (resError);
  125.        }
  126.     }
  127.  
  128.     return (err);
  129. }
  130.  
  131. /*****************************************************************
  132.  *    GetOurZoneName
  133.  *
  134.  *  Calls the AppleTalk PGetAppleTalkInfo call to retrieve the zone name for this
  135.  *  machine.
  136.  *****************************************************************/
  137. OSErr GetOurZoneName(StringPtr zonePtr)
  138. {
  139. MPPParamBlock  pb;
  140. OSErr            err;
  141.  
  142.     pb.GAIINFO.version = 1;  /* assume Phase 2 drivers */
  143.     pb.GAIINFO.LAlength = 0;
  144.     pb.GAIINFO.linkAddr = NULL;
  145.     pb.GAIINFO.zoneName = zonePtr;
  146.  
  147.     err = PGetAppleTalkInfo(&pb, false);
  148.     if (err == noErr)
  149.     {    /* is there no zone name? */
  150.        if ((*(unsigned char *)(*zonePtr)) == 0)
  151.           /* specify "this" zone */
  152.           BlockMove(gThisZone,
  153.                      zonePtr,
  154.                      gThisZone[0]+1);
  155.     }
  156.     return (err);
  157. }
  158.